package com.cl.blog.mq;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cl.blog.entity.BCommunity;
import com.cl.blog.entity.BContent;
import com.cl.blog.entity.BNewNotice;
import com.cl.blog.exception.WebException;
import com.cl.blog.service.BCommunityService;
import com.cl.blog.service.BContentService;
import com.cl.blog.service.BNewNoticeService;
import com.cl.blog.util.RedisUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;

import static com.cl.blog.config.RabbitMqConfig.*;

/**
 * @Author 付
 * @Date 2024-04-01 11:28
 * @Version 1.0
 * @description 死信队列消费者
 * @since 1.0
 */
@Slf4j
//@Component
public class DeadLetterQueueConsumer {

	@Autowired
	BContentService bContentService;
	@Autowired
	RedisUtil redisUtil;
	@Autowired
	BCommunityService bCommunityService;
	@Autowired
	BNewNoticeService bNewNoticeService;

	@RabbitListener(queues = DLX_QUEUE_NAME)
	public void handle(Message message, Channel channel) throws IOException {
		byte[] body = message.getBody();
		String s = new String(body, StandardCharsets.UTF_8);
		log.info("违禁词--->{}", s);
		bCommunityService.update(new UpdateWrapper<BCommunity>().eq("community_id", s).set("status",1));
		// 通告
		channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
	}

    /**
        消息ack
            basicAck(long deliveryTag,boolean multiple) 
                消息确认应答，当发送之后，rabbitmq会认为已经处理该消息，丢弃消息
                multiple: 是否应用于多消息
            basicReject(long deliveryTag,boolean requeue) 
                否定确认应答，reject之后，消费者还是会消费到该消息
                requeue：是否重入队列，true则重新插入队列，false则丢弃或者进入死信队列
            basicNack(long deliveryTag,boolean multiple,boolean requeue)
                否定确认，已拒绝处理该消息，可以将其丢弃
            basicRecover(boolean requeue)
                是否恢复到消息队列
                requeue：ture则重入队列，并且尽可能的将recover的消息推送给其它消费者，而不是由自己再消费。false则消息会被重新投递到自己
    */

	@RabbitListener(queues = DELAYED_QUEUE_NAME)
	public void receiveD(Message message, Channel channel) throws IOException {
		String msg = new String(message.getBody());
		log.info("当前时间：{},延时队列收到消息：{}", new Date().toString(), msg);
		System.out.println("获取的key-->" + msg);
		String[] split = msg.split("-");
		System.out.println("获取的文章信息-->"+split);
		String userId = split[0];
		String contentId = split[1];
		System.out.println(split);
		BContent bContent = bContentService.getOne(new QueryWrapper<BContent>()
				.eq("user_id", Integer.valueOf(userId))
				.eq("content_id", contentId)
		);
		if (bContent == null) {
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
			return;
		}
		boolean update = bContentService.update(new UpdateWrapper<BContent>()
				.eq("user_id", Integer.valueOf(userId))
				.eq("content_id", contentId)
				.set("status", 1)
		);
		System.out.println("修改结果 = " + update);
		channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
	}
}
